//
//  XDDataPicker.m
//  XDEducation
//
//  Created by apple on 2017/5/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDDataPicker.h"

@interface XDDataPicker ()

/** dataPicker */
@property(nonatomic,strong) UIDatePicker *datePicker;

/** 确定按钮 */
@property(nonatomic,strong) UIButton *sureBtn;

/** 取消按钮 */
@property(nonatomic,strong) UIButton *cancelBtn;
@end

@implementation XDDataPicker

-(instancetype)init{

    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        
        
        //添加 确定\取消 按钮
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 5, 60, 30)];
        [_cancelBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_cancelBtn];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        
        
        _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 15 - 60, 5, 60, 30)];
        [_sureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_sureBtn];
        [_sureBtn addTarget:self action:@selector(sureBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        
        
        _datePicker = [[UIDatePicker alloc]init];
        _datePicker.datePickerMode = UIDatePickerModeTime;
        [_datePicker setLocale:[NSLocale currentLocale]];
        [self addSubview:_datePicker];
        
        
    }
    return self;
}

-(void)layoutSubviews{

    _datePicker.frame = CGRectMake(0, 40, ScreenW, self.height - 40);
}

-(void)setMaximumDate:(NSDate *)maximumDate{
    _maximumDate = maximumDate;
    _datePicker.maximumDate = maximumDate;
}

-(void)setMinimumDate:(NSDate *)minimumDate{
    _minimumDate = minimumDate;
    _datePicker.minimumDate = minimumDate;
}

-(void)setDatePickerMode:(UIDatePickerMode)datePickerMode{
    _datePickerMode = datePickerMode;
    _datePicker.datePickerMode = datePickerMode;
}

-(void)cancelBtnClicked{

    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

-(void)sureBtnClicked{

    NSDate *select  = [_datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *time = [dateFormatter stringFromDate:select];
    NSLog(@"%@",time);
    
    if (self.sureBlock) {
        self.sureBlock(time);
    }
    
}

@end
